package org.medusa.sample;

import java.util.Date;

import org.medusa.core.EventCallback;
import org.medusa.core.EventPump;
import org.medusa.log.ILog;

public class MyLog implements ILog {

	private int lv;
	private EventPump evp = null;
	private long EVCODE_LOG_OUTPUT;

	public MyLog(int lv) {
		this.lv = lv;
		this.evp = new EventPump("log");
		this.EVCODE_LOG_OUTPUT = this.evp
				.AddEventListener(new EventCallback<Object[]>() {

					@Override
					public void execute(Object[] args) {
						int lv = (Integer) args[0];
						String heading = (String) args[1];
						String info = "";
						if (args.length < 3)
							return;
						Object[] objs = (Object[]) args[2];
						if (objs != null)
							for (int i = 0; i < objs.length; i++) {
								Object obj = objs[i];
								if (obj != null)
									info += "" + objs[i].toString();
							}
						@SuppressWarnings("deprecation")
						String time = new Date().toLocaleString();
						if(lv<=ILog.WarningLv)
							System.err.println(time+" [" + heading + "] " + info);
						else
							System.out.println(time+" [" + heading + "] " + info);
					}

					@Override
					public void removed() {
						// TODO Auto-generated method stub
						
					}

				});

	}

	public int GetLevel() {
		return this.lv;
	}

	public void Error(String heading, Object... args) {
		if (this.lv >= ILog.ErrorLv) {
			this.evp.DispatchEvent(this.EVCODE_LOG_OUTPUT, ILog.ErrorLv, heading, args);
		}
	}

	public void Warning(String heading, Object... args) {
		if (this.lv >= ILog.WarningLv) {
			this.evp.DispatchEvent(this.EVCODE_LOG_OUTPUT, ILog.WarningLv, heading, args);
		}
	}

	public void Success(String heading, Object... args) {
		if (this.lv >= ILog.SuccessLv) {
			this.evp.DispatchEvent(this.EVCODE_LOG_OUTPUT, ILog.SuccessLv, heading, args);
		}
	}

	public void Debug(String heading, Object... args) {
		if (this.lv >= ILog.DebugLv) {
			this.evp.DispatchEvent(this.EVCODE_LOG_OUTPUT, ILog.DebugLv, heading, args);
		}
	}

	public void Stack(String heading, Object... args) {
		if (this.lv >= ILog.Debug2Lv) {
			this.evp.DispatchEvent(this.EVCODE_LOG_OUTPUT, ILog.Debug2Lv, heading, args);
		}
	}

	public void Temp(String heading, Object... args) {
		if (this.lv >= ILog.Debug3Lv) {
			this.evp.DispatchEvent(this.EVCODE_LOG_OUTPUT, ILog.Debug3Lv, heading, args);
		}
	}

	public void Join() {
		this.evp.Join();
	}

	public void Close() {
		this.evp.Stop();
	}
}
